package bidsanity

class CustomerController {
    def customerService


    def index() {
        [customers: Customer.list(), customerInstanceTotal: Customer.count()]
    }

    def create() {
        [customerInstance: new Customer(params)]
    }
    
    def show(){
        def customerInstance = Customer.get(params.id)
        if (!customerInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'customer.label', default: 'Customer'), params.id])
            redirect(action: "index")
            return
        }
        [customer: customerInstance]
    }

    def delete(){
        if (!session.user) {
            flash.message = message(code: 'customer.not.logged.in')
            redirect(action: "index")
            return
        }
        def customerInstance = Customer.get(params.id)
        if (!customerInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'customer.label', default: 'Customer'), params.id])
            redirect(action: "list")
            return
        }
        def user = Customer.findByEmailAddress(session.user)
        def customerIsSessionUser = (customerInstance.id == user.id)
        if (!user.isAdmin && !customerIsSessionUser){
            flash.message = message(code: 'customer.not.self')
            redirect(action: "index")
            return
        }
        if(Bid.findAllByBidder(customerInstance).size() > 0)
        {
            flash.message = message(code: 'customer.bids.not.empty', args: [customerInstance.emailAddress])
            redirect(action: "show", id: customerInstance.id)
        }
        else if (Listing.findAllBySeller(customerInstance).size() > 0)
        {
            flash.message = message(code: 'customer.has.listing', args: [customerInstance.emailAddress])
            redirect(action: "show", id: customerInstance.id)
        }
        else if (customerInstance.isAdmin) {
            flash.message = message(code: 'customer.is.admin', args: [customerInstance.emailAddress])
            redirect(action: "show", id: customerInstance.id)
        }
        else
        {
            customerInstance.delete(failOnError: false)
            if (customerInstance.hasErrors())
            {
                redirect(action: "index")
            }
            else
            {
                if (customerIsSessionUser) {session.user = null} //log them out if they delete their own account.
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'customer.label', default: 'Customer'), customerInstance.id])
                redirect(action: "index")
            }
        }
    }    
    
    
    def save() {
        //can't create admin accounts here!
        if (params.isAdmin != null && params.isAdmin == true) {
            flash.message = message(code: 'default.not.created.message', args: [message(code: 'customer.label', default: 'Customer'), params.emailAddress])
            render(view: "create", model: [customerInstance: customerInstance])
            return
        }
        def customerInstance = customerService.createCustomer(params)
        if (customerInstance.id == null) {
            flash.message = message(code: 'default.not.created.message', args: [message(code: 'customer.label', default: 'Customer'), params.emailAddress])
            render(view: "create", model: [customerInstance: customerInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'customer.label', default: 'Customer'), customerInstance.emailAddress])
        redirect(action: "login", id: customerInstance.id)
    }

    def edit() {
        if (!session.user) {
            flash.message = message(code: 'customer.not.logged.in')
            redirect(action: "index")
            return
        }
        def customerInstance = Customer.get(params.id)
        if (!customerInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'customer.label', default: 'Customer'), params.id])
            redirect(action: "list")
            return
        }
        def user = Customer.findByEmailAddress(session.user)
        if (!user.isAdmin && customerInstance.id != user.id){
            flash.message = message(code: 'customer.not.self')
            redirect(action: "index")
            return
        }

        [customerInstance: customerInstance]
    }

    def update() {
        if (!session.user) {
            flash.message = message(code: 'customer.not.logged.in')
            redirect(action: "index")
            return
        }
        def customerInstance = Customer.get(params.id)
        if (!customerInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'customer.label', default: 'Customer'), params.id])
            redirect(action: "list")
            return
        }
        def user = Customer.findByEmailAddress(session.user)
        def customerIsSessionUser = (customerInstance.id == user.id)
        if (!user.isAdmin && !customerIsSessionUser){
            flash.message = message(code: 'customer.not.self')
            redirect(action: "index")
            return
        }

        if (params.version) {
            def version = params.version.toLong()
            if (customerInstance.version > version) {
                customerInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                        [message(code: 'customer.label', default: 'Customer')] as Object[],
                        "Another user has updated this Customer while you were editing")
                render(view: "edit", model: [customerInstance: customerInstance])
                return
            }
        }

        customerInstance.properties = params

        if (!customerInstance.save(flush: true)) {
            render(view: "edit", model: [customerInstance: customerInstance])
            return
        }

        if (customerIsSessionUser) {
            session.user = customerInstance.emailAddress
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'customer.label', default: 'Customer'), customerInstance.id])
        redirect(action: "show", id: customerInstance.id)
    }

    def login(){
        if (request.method == "POST"){
            def user = Customer.findByEmailAddress(params.emailAddress)
            if((user) && (user.password == params.password)){
                session.user = user.emailAddress
                if (user.isAdmin) { redirect(controller: 'listing', action: 'index') }
                else {redirect(controller: 'listing', action: 'myListings')}
                return
            }
            flash.message = message(code: 'customer.login.incorrect')
            render view: 'login'
        }
    }

    def logout(){
        session.user = null
        redirect(controller: 'home', action: 'index')
    }
}
